import Toast from '../../../../miniprogram_npm/@vant/weapp/toast/toast';
import * as Storage from '../../../../utils/storage';
const app = getApp();
const $api = require('../../../../request/api').API;
// pages/person/authPromoter/index.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    phone: '',
    isPhone: true,
    // code:"",
    // seconds:"",
    checked: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let scene = decodeURIComponent(options.scene);
    if (scene != 'undefined') {
      this.code = scene.split('cPromote=')[1];
      app.isLogin(this.judge);
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},
  judge() {
    const detailRole = wx.getStorageSync(Storage.DETAIL_ROLE);
    if (detailRole == 3 || detailRole == 2) {
      Toast.fail({
        message: detailRole == 3 ? '业务员不能扫推广码' : '已经是推广员',
        forbidClick: true
      });
      setTimeout(() => {
        wx.switchTab({
          url: '/pages/person/index'
        });
      }, 2000);
    } else {
      // 判断是否绑定手机号
      this.getUserPhone();
    }
  },
  onChange(e) {
    this.setData({
      checked: e.detail
    });
  },
  getUserPhone() {
    $api.getUserPhone().then(res => {
      const data = res.data.data;
      this.setData({
        isPhone: data ? true : false,
        phone: data
      });
    });
  },
  getPhoneNumber(e) {
    $api.getPhone({ code: e.detail.code, appId: Storage.APPID }).then(res => {
      const data = res.data.data;
      if (data) {
        const params = {
          code: this.code,
          phone: data,
          type: 4,
          openId: wx.getStorageSync(Storage.USER_OPEN_ID)
        };
        $api
          .customerScanCode(params)
          .then(res => {
            Toast.success({
              forbidClick: true,
              message: '设置成功'
            });
            wx.setStorageSync(Storage.DETAIL_ROLE, 2);
            if (res.data.data) wx.setStorageSync(Storage.LEVEL, res.data.data.level);
            setTimeout(() => {
              wx.reLaunch({
                url: '/pages/person/index'
              });
            }, 1000);
          })
          .catch(err => {
            wx.switchTab({
              url: '/pages/person/index'
            });
          });
      }
    });
  },
  submit() {
    const { checked, phone } = this.data;
    if (!checked) {
      Toast.fail('请先勾选协议');
      return;
    }
    const params = {
      code: this.code,
      type: 4,
      openId: wx.getStorageSync(Storage.USER_OPEN_ID)
    };
    if (phone) {
      params.phone = phone;
    }
    $api
      .customerScanCode(params)
      .then(res => {
        Toast.success({
          forbidClick: true,
          message: '设置成功'
        });
        wx.setStorageSync(Storage.DETAIL_ROLE, 2);
        if (res.data.data) wx.setStorageSync(Storage.LEVEL, res.data.data.level);
        setTimeout(() => {
          wx.reLaunch({
            url: '/pages/person/index'
          });
        }, 1000);
      })
      .catch(err => {
        setTimeout(() => {
          wx.switchTab({
            url: '/pages/person/index'
          });
        }, 2000);
      });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {}
});
